package ruifeng.zcj.dig.ui.shopping;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.greenrobot.eventbus.Subscribe;

import java.io.Serializable;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Base;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.shopping.bean.ThreeCategory;
import ruifeng.zcj.dig.ui.shopping.bean.TwoCategory;
import ruifeng.zcj.mybase.base.utils.GlideUtil;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * Created by Administrator on 2016/9/29 0029.
 */
public class ThreeCategoryFragment extends CategoryFragment {

    private static final String CACHE_KEY_PREFIX = "ThreeCategoryFragment";
    ThreeCategory category = null;
    TwoCategory.RowsBean rowsBean = null;
    ShoppingActivity shoppingActivity;


    /**
     * 配置eventbus
     **/
    protected void initUIConfig(FragmentConfig fragmentConfig) {
        fragmentConfig.isApplyEventBus = true;
    }

    protected void sendRequestData() {
        OSChinaApi.getsuppliergoodstypelist(categoryType(), supplierid(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    sendRequestDataSucess(responseInfo.result);
                    if (isAdded()) {
                        if (mState == STATE_REFRESH) {
                            onRefreshNetworkSuccess();
                        }
                        Base base = fromJson(responseInfo.result);
                        // 保存数据
                        saveObject(getCacheKey(), base);
                        executeOnLoadDataSuccess(base);// 读取数据成功
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter(recyclerView);
    }

    @Override
    protected String getCacheKeyPrefix() {
        if (rowsBean != null) {
            String cacheKey = rowsBean.getSubtiertype().replaceAll("/", "_");
            return CACHE_KEY_PREFIX + rowsBean.getSubtierid() + cacheKey;
        }
        return CACHE_KEY_PREFIX;
    }

    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);

        Bundle bundle = getArguments();

        shoppingActivity = (ShoppingActivity) mActivity;
    }

    protected String supplierid() {
        if (rowsBean != null)
            return rowsBean.getSubtierid();
        else return null;
    }

    /***
     * 把数据显示到view上面去
     **/
    @Override
    protected void executeOnLoadDataSuccess(Serializable seri) {
        super.executeOnLoadDataSuccess(seri);
        category = (ThreeCategory) seri;
        if (mMomentAdapter != null) {
            mMomentAdapter.addNewData(category.getRows());
        }
    }

    @Override
    protected int categoryType() {
        return 2;
    }

    @Override
    protected Base fromJson(String netResult) {
        return (ThreeCategory) AbJsonUtil.fromJson(netResult, ThreeCategory.class);
    }


    public class MomentAdapter extends BGARecyclerViewAdapter<ThreeCategory.RowsBean> {
        public MomentAdapter(RecyclerView recyclerView) {
            super(recyclerView, R.layout.listview_item_category);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, ThreeCategory.RowsBean moment) {
            /*if (TextUtils.isEmpty(moment.content)) {
                helper.setVisibility(R.id.tv_item_moment_content, View.GONE);
            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/
            ImageView imageView = helper.getImageView(R.id.vehicle_fragment_item_img);

            GlideUtil.loadImage(mContext, String.format("http://kw.runfkj.com%s", moment.getPhoto()), imageView);

            helper.setText(R.id.vehicle_fragment_item_title, " " + moment.getSumciertype());
            //  helper.setText(R.id.vehicle_fragment_item_address, "齿轮总成/星星齿");
            helper.setVisibility(R.id.vehicle_fragment_item_address, View.GONE);

          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }

    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {
        if (category != null) {
            ThreeCategory.RowsBean rowsBean = category.getRows().get(position);
            FragmentArgs fragmentArgs2 = new FragmentArgs();
            fragmentArgs2.add("ThreeCategory", rowsBean);
            ToActivityUtil.toNextActivityAndFinish(getContext(), ShoppingListActivity.class, "threecategoryBundle", fragmentArgs2);
        }
    }

    @Override
    public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
        return false;
    }

    /**
     * 与发布者在同一个线程
     * 注意事项：
     * 1.事件被post后哪个事件的监听者会响应的问题，这个取决于你写的监听方法所跟的参数类型。
     * 2.你写的事件监听方法得是public修饰的，如果你写成private了，则不会收到消息。
     * 3.如果你所有的事件监听方法都不用public修饰（包括父类），则会直接报错：
     *
     * @param rowsBean 事件2
     */
    @Subscribe
    public void onEvent(TwoCategory.RowsBean rowsBean) {
        this.rowsBean = rowsBean;
        if (shoppingActivity != null){
            shoppingActivity.mActivityTopCenter.setText(""+rowsBean.getSubtiertype());
        }
    }
}
